<template>
  <div style="padding: 20px;">
    <h2 style="margin-bottom: 20px;">股票净利润详情</h2>
    <el-descriptions :column="3" border v-loading="loading">
      <el-descriptions-item label="股票代码" :span="1">
        {{ stockProfitDetail.stockCode }}
      </el-descriptions-item>
      <el-descriptions-item label="会计年度" :span="2">
        {{ stockProfitDetail.fiscalYear }}
      </el-descriptions-item>
      
      <el-descriptions-item label="Q1净利润" :span="1">
        {{ formatNumber(stockProfitDetail.q1NetProfit) }}
      </el-descriptions-item>
      <el-descriptions-item label="Q1扣非净利润" :span="1">
        {{ formatNumber(stockProfitDetail.q1NoneNetProfit) }}
      </el-descriptions-item>
      <el-descriptions-item label="Q1每股收益" :span="1">
        {{ formatEps(stockProfitDetail.q1Eps) }}
      </el-descriptions-item>
      
      <el-descriptions-item label="Q2净利润" :span="1">
        {{ formatNumber(stockProfitDetail.q2NetProfit) }}
      </el-descriptions-item>
      <el-descriptions-item label="Q2扣非净利润" :span="1">
        {{ formatNumber(stockProfitDetail.q2NoneNetProfit) }}
      </el-descriptions-item>
      <el-descriptions-item label="Q2每股收益" :span="1">
        {{ formatEps(stockProfitDetail.q2Eps) }}
      </el-descriptions-item>
      
      <el-descriptions-item label="Q3净利润" :span="1">
        {{ formatNumber(stockProfitDetail.q3NetProfit) }}
      </el-descriptions-item>
      <el-descriptions-item label="Q3扣非净利润" :span="1">
        {{ formatNumber(stockProfitDetail.q3NoneNetProfit) }}
      </el-descriptions-item>
      <el-descriptions-item label="Q3每股收益" :span="1">
        {{ formatEps(stockProfitDetail.q3Eps) }}
      </el-descriptions-item>
      
      <el-descriptions-item label="Q4净利润" :span="1">
        {{ formatNumber(stockProfitDetail.q4NetProfit) }}
      </el-descriptions-item>
      <el-descriptions-item label="Q4扣非净利润" :span="1">
        {{ formatNumber(stockProfitDetail.q4NoneNetProfit) }}
      </el-descriptions-item>
      <el-descriptions-item label="Q4每股收益" :span="1">
        {{ formatEps(stockProfitDetail.q4Eps) }}
      </el-descriptions-item>
    </el-descriptions>
    <div style="margin-top: 20px;">
      <el-button type="primary" @click="handleEdit">编辑</el-button>
      <el-button @click="handleBack">返回</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'StockProfitView',
  data() {
    return {
      stockProfitDetail: {
        id: '',
        stockCode: '',
        fiscalYear: null,
        q1NetProfit: null,
        q1NoneNetProfit: null,
        q1Eps: null,
        q2NetProfit: null,
        q2NoneNetProfit: null,
        q2Eps: null,
        q3NetProfit: null,
        q3NoneNetProfit: null,
        q3Eps: null,
        q4NetProfit: null,
        q4NoneNetProfit: null,
        q4Eps: null
      },
      loading: false
    }
  },
  created() {
    const id = this.$route.query.id;
    if (id) {
      this.loadStockProfitDetail(id);
    } else {
      this.$message.warning('缺少股票净利润ID参数');
      this.$router.push({ name: 'StockProfit' });
    }
  },
  methods: {
    formatNumber(value) {
      if (value === null || value === undefined) {
        return '-';
      }
      return value.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
    },
    // 格式化每股收益（保留4位小数）
    formatEps(value) {
      if (value === null || value === undefined) {
        return '-';
      }
      return value.toLocaleString('zh-CN', { minimumFractionDigits: 4, maximumFractionDigits: 4 });
    },
    loadStockProfitDetail(id) {
      console.log('loadStockProfitDetail', id);
      this.loading = true;
      this.request.get("/stockprofit/" + id).then(res => {
        this.loading = false;
        if (res.code === 200) {
          this.stockProfitDetail = res.data;
        }
      }).catch(() => {
        this.loading = false;
        this.$message.error('加载数据失败');
      });
    },
    handleEdit() {
      this.$router.push({
        name: 'StockProfitForm',
        query: { id: this.stockProfitDetail.id }
      });
    },
    handleBack() {
      this.$router.push({ name: 'StockProfit' });
    }
  }
}
</script>

<style scoped>
</style>